# -*- coding: UTF-8 -*-    
# Author:yangshuoshuo
# FileName:practice_02
# DateTime:2024/5/26 15:44
# SoftWare: PyCharm

"""
作业要求:编写一个Python程序，将一些文本内容写入到文件中，并且能够从文件中读取内容并显示出来。
file_name = "my_file.txt"
content = "这是一个文本文件测试"

with open(file_name, 'w', encoding='utf-8') as f:
    f.write(content)
    print(f"'{content}'已经被写入文件{file_name}")


with open(file_name, 'r', encoding='utf-8') as f:
    file_content = f.read()
    print(f"文件内容为'{file_content}'")
"""

"""
编写程序，让用户输入两个整数start和end,然后输出这两个整数之间的一个随机数。要求考虑用户输入不是整数的情况，以及start>end的情况。
根据实际情况进行适当提示或输出。

import random

try:
    start = int(input("请输入一个整数作为开始值："))
    end = int(input("请输入一个整数作为结束值；"))

    if start > end:
        raise ValueError("起始值不能大于结束值，请重新输入。")

    random_num = random.randint(start, end)
except ValueError as ve:
    print(ve)
except Exception as e:
    print(f"发生了一个错误：{e}")
"""

"""
定义一个商品类，包含 商品名称 和 商品价格 两个属性，
实现商品类的对象描述方法和添加到购物车方法
定义一个购物车类，包含一个商品列表 属性，和 添加商品 及 显示所有商品 两个方法

class Product(object):
    def __init__(self, name, price):
        self.price = price
        self.name = name

    def __str__(self):
        return f"Product:{self.name}, Price: {self.price}"

    def add_car(self, cart):
        cart.add_product(self)


class Shopping_Cart(object):
    def __init__(self):
        self.products = []

    def add_product(self, product):
        self.products.append(product)

    def show_products(self):
        for i in self.products:
            print(i)


if __name__ == '__main__':
    product1 = Product("Apple", 5.0)
    product2 = Product("Banana", 3.0)
    product3 = Product("Orange", 4.0)

    cart = Shopping_Cart()

    product1.add_car(cart)
    product2.add_car(cart)
    product3.add_car(cart)

    print(f"购物车内容：")
    cart.show_products()
"""

"""
小明和小美都爱跑步
小明体重 75 公斤
小美体重 45 公斤
每次跑步会减肥 0.5 公斤
每次吃东西体重增加 1 公斤
请根据打印出跑完步之后的体重

class Person(object):
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight

    def __str__(self):
        return f"姓名：{self.name}, 体重：{self.weight}"

    def run(self):
        print(f"{self.name}在跑步，体重降低0.5公斤")
        self.weight -= 0.5

    def eat(self):
        print(f"{self.name}在吃饭，体重增加1公斤")
        self.weight += 1


if __name__ == '__main__':
    xiao_ming = Person("小明", 75)
    print(xiao_ming)
    xiao_ming.eat()
    xiao_ming.run()
    print(xiao_ming)

    xiao_mei = Person("小美", 45)
    print(xiao_mei)
    xiao_mei.eat()
    xiao_mei.run()
    print(xiao_mei)
"""

"""
编写一个Python程序： 
- 1.房子有户型，总面积和家具名称列表，新房子没有任何的家具。 
- 2.家具有名字和占地面积，其中 - 床：占4平米 - 衣柜：占2平米 - 餐桌：占1.5平米 
- 3.将以上三件家具添加到房子中 
- 4.打印房子时，要求输出:户型，总面积，剩余面积，家具名称列表


class House(object):
    def __init__(self, house_type, total_area):
        self.house_type = house_type
        self.total_area = total_area
        self.furniture = []

    def add_furniture(self, furniture):
        if self.total_area >= furniture.area:
            self.furniture.append(furniture)
            self.total_area -= furniture.area
            print(f"房子剩余面积{self.total_area}")

        else:
            print(f"房子剩余面积不足，无法添加{furniture.name}")

    def display(self):
        print(f"房子户型：{self.house_type}")
        print(f"房子总面积：{self.total_area}")
        print("房子家具列表：")
        for furniture in self.furniture:
            print(furniture)


class Furniture(object):
    def __init__(self, name, area):
        self.name = name
        self.area = area

    def __str__(self):
        return f"{self.name},占地面积{self.area}"


if __name__ == '__main__':
    my_house = House("两室一厅", 100)
    my_house.display()
    print(f"{'*' * 10}")

    bed = Furniture("床", 4)
    wardrobe = Furniture("衣柜", 2)
    table = Furniture("餐桌", 1.5)

    print()
    my_house.add_furniture(bed)
    my_house.add_furniture(wardrobe)
    my_house.add_furniture(table)
    my_house.display()
"""

"""
编写一个Python程序:青蛙跳台阶，共有10阶台阶，青蛙每次可以选择跳一阶或者两阶，
问：青蛙跳上这10个台阶共有多少种跳法。

class Frog(object):
    def __init__(self):
        self.demo = {}

    def jump(self, n):
        if n in self.demo:
            return self.demo.get(n)
        elif n == 1:
            self.demo[n] = 1
            return 1
        elif n == 2:
            self.demo[n] = 2
            return 2
        else:
            result = self.jump(n - 1) + self.jump(n - 2)
            self.demo[n] = result
            return result


frog = Frog()
frog.jump(10)
print(frog.demo.get(10))
print(frog.demo)
"""

"""
对 get/post/put/delete四种请求方式分别封装对应的请求方法
参数有 `url, header(使用字典)`，`callback`回调函数
`callback`回调整函数中，用来显示前两个参数的信息



class RequestTools(object):
    @classmethod
    def get(cls, url, header, callback):
        callback(url, header)

    @classmethod
    def post(cls, url, header, callback):
        callback(url, header)

    @classmethod
    def put(cls, url, header, callback):
        callback(url, header)

    @classmethod
    def delete(cls, url, header, callback):
        return callback(url, header)


def func(url, header):
    print(f"请求地址：{url}")
    print(f"请求的信息有：")
    for i in header:
        print(f"{i}:{header[i]}")


if __name__ == '__main__':
    RequestTools.get('https://www.baidu.com', {"content-type": "text/html"}, func)
    RequestTools.post('https://www.baidu.com', {"content-type": "text/html"}, func)
    RequestTools.put('https://www.baidu.com', {"content-type": "text/html"}, func)
    RequestTools.delete('https://www.baidu.com', {"content-type": "text/html"}, func)

"""

"""
编写一个Python程序，创建一个几何图形计算程序，使用静态方法来计算矩形的面积和周长


class Geometry(object):
    @staticmethod
    def rectangle_area(width, height):
        return width * height

    @staticmethod
    def rectangle_perimeter(width, height):
        return (width + height) * 2


if __name__ == '__main__':
    width1 = 4
    height1 = 6
    print(f"矩形宽：{width1},矩形高：{height1},矩形面积：{Geometry.rectangle_area(width1,height1)},"
          f"矩形周长：{Geometry.rectangle_perimeter(width1, height1)}")
          
          
class Hero(object):
    def __init__(self, name, hp, power):
        self.name = name
        self.__hp = hp
        self.power = power

    def speak(self):
        print(f"欢迎来到英雄联盟，我的名字是{self.name}，我的血量是{self.__hp},我的攻击力是{self.power}")

    @property
    def hp(self):
        return self.__hp


class ApcHero(Hero):
    def __init__(self, name, hp, power, mp):
        super(ApcHero, self).__init__(name, hp, power)
        self.mp = mp

    def speak(self):
        super(ApcHero, self).speak()
        print(f"我是{self.name}大美女")

    def charm(self):
        if self.mp < 50:
            print("蓝量不足")

        else:
            print('施展魅惑技能')
            self.mp -= 50


class WarriorHero(Hero):
    def __init__(self, name, hp, power, armor):
        super(WarriorHero, self).__init__(name, hp, power)
        self.armor = armor


def fight(hero1: Hero, hero2: Hero):
    hero1.speak()
    hero2.speak()
    hero1_hp = hero1.hp
    hero2_hp = hero2.hp
    hero1_name = hero1.name
    hero2_name = hero2.name

    while True:
        hero1_hp -= 10
        hero2_hp -= 10

        if hero1_hp <= 0 or hero2_hp <= 0:
            print(f"{hero1_name}的血量为{hero1_hp}")
            print(f"{hero2_name}的血量为{hero2_hp}")
            if hero1_hp > hero2_hp:
                print(f"{hero1_name}赢了")

            elif hero1_hp < hero2_hp:
                print(f"{hero2_name}赢了")

            else:
                print("平局")
            break


def test_fight():
    jinx = Hero("金克斯", 1000, 100)
    dc = ApcHero("貂蝉", 1200, 100, 100)
    fight(jinx, dc)


class HeroFactory(object):
    @staticmethod
    def create_hero(hero_type, *args):
        if hero_type == "apc":
            return ApcHero(*args)
        elif hero_type == "warrior":
            return WarriorHero(*args)
        else:
            return Hero(*args)


if __name__ == '__main__':
    test_fight()
"""

"""
设计一个简单的动物园系统，其中包含不同类型的动物（如狗、猫和鸟）。每个动物都有自己的属性（如名字、年龄）和行为（如发出声音）。
使用封装、继承和多态来完成。


class Animal(object):
    def __init__(self, name, age):
        self.__name = name
        self.__age = age

    @property
    def name(self):
        return self.__name

    @property
    def age(self):
        return self.__age

    def make_sound(self):
        pass


class Dog(Animal):
    def __init__(self, name, age):
        super(Dog, self).__init__(name, age)

    def make_sound(self):
        print(f"{self.name}发出汪汪叫声")


class Cat(Animal):
    def __init__(self, name, age):
        super(Cat, self).__init__(name, age)

    def make_sound(self):
        print(f"{self.name}发出喵喵叫声")


class Bird(Animal):
    def __init__(self, name, age):
        super(Bird, self).__init__(name, age)

    def make_sound(self):
        print(f"{self.name}发出鸟叫声")


if __name__ == '__main__':
    dog = Dog("狗狗", 3)
    cat = Cat("猫猫", 2)
    bird = Bird("喜鹊", 1.5)

    dog.make_sound()
    cat.make_sound()
    bird.make_sound()
"""

"""
import math

print(math.pi)
print(math.tau)
print(math.sqrt(4))
print(math.ceil(3.14))
print(math.floor(3.14))
"""
